package com.aliyun.messagemgt.rocketmq;

import com.alibaba.fastjson.JSON;
import com.aliyun.messagemgt.model.AsyncPushRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.annotation.ExtRocketMQTemplateConfiguration;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

/**
 * @author Qinyuan
 * @date 2025/7/29
 */
@Component
@Slf4j
@ExtRocketMQTemplateConfiguration(group = "${rocketmq.producer.group}")
public class MessageInnerForwardMQTemplate extends RocketMQTemplate {

    @Value("${rocketmq.producer.topic}")
    private String topic;

    public void asyncSend(AsyncPushRequest pushRequest) {
        String msg = JSON.toJSONString(pushRequest);
        asyncSend(topic, MessageBuilder.withPayload(msg).build(),
                new SendCallback() {
                    @Override
                    public void onSuccess(SendResult sendResult) {
                        log.info("inner forward send success");
                    }

                    @Override
                    public void onException(Throwable throwable) {
                        log.error("inner forward send error:{}", msg, throwable);
                    }
                });
    }

}
